<?php
if(!defined('IN_MICECMS')){header("HTTP/1.0 404 Not Found");die();}
function MenuList($micefun,$menuid){
	if($menuid){
		$menu=new model_Category();
		$menu->setSubsection('WHERE isvisible=0 AND type=\'menu\' AND parentid='.$menuid.' ORDER BY orderid ASC');
		$menus=$menu->doSelect();
	}else{
		$menus=new model_Category();
	}
	return $menus;
}

function CategoryName($micefun,$str){
	$category=new model_Category();
	$category->setSubsection('WHERE isvisible=0 AND type LIKE \'article%\' ORDER BY category_id');
	$categoryrs=$category->doSelect();

	$categoryname='';
	foreach($categoryrs as $category){
		if(strstr($str,','.$category->getCategoryId().',')){
			if($categoryname)	$categoryname.="<br>";
			$categoryname.=$category->getTitle();
		}
	}
	return $categoryname;
}

function LinkCategoryName($micefun,$str){
	$category=new model_Category();
	$category->setSubsection('WHERE isvisible=0 AND type = \'link\' ORDER BY category_id');
	$categoryrs=$category->doSelect();

	$categoryname='';
	foreach($categoryrs as $category){
		if(strstr($str,','.$category->getCategoryId().',')){
			if($categoryname)	$categoryname.="<br>";
			$categoryname.=$category->getTitle();
		}
	}
	if(!$categoryname){
		$categoryname='默认分类';
	}
	return $categoryname;
}

function CategoryId($micefun,$str){
	$strs=explode(',',$str);
	return $strs[sizeof($strs)-2];
}

function GroupName($micefun,$str){
	$group=new model_Group();
	$group->setSubsection('WHERE group_id='.$str);
	$results=$group->doSelectOne();
	
	return $results->getName();
}

//执行SQL查询,返回html结果
function record_html($query,$num) {
	global $conn;
	$query=str_replace("`","",$query);
	$body = '<h3>'.$query.'</h3>';
	//执行sql
	$rs=@$conn->RawExecute($query);
	if ($rs===false) $body .= '<p style="color:red">';
	else $body.='<p>';
	// 显示返回信息
	$body .= $conn->ErrorMsg();
	$body .=   '</p>';
	if (is_bool($rs))
		return $body;
	$body .=  'Affected Rows: '.$conn->Affected_Rows().' - ';
	$row_total = $rs->RecordCount();
	if (!$row_total) return $body;
	$body .=  'Result rows: '.$row_total.' - ';
	$body .=  'Fields: '.$rs->FieldCount();
	$body.= '<table   border="0" cellpadding="0" cellspacing="1" bgcolor="#666666">';
	$body .= "<thead><tr>\n";
	$rsa = $rs->GetArray();	$rs = null;
	foreach(array_keys($rsa[0]) as $col)
		$body.='<th  bgcolor="#FFFFFF">'.$col."</th>\n";
	$body .= "</tr></thead>\n";
	reset($rsa);
	foreach($rsa as $row) {
		$body .="<tbody><tr>\n";
		foreach($row as $val)
			$body .= '<td  bgcolor="#FFFFFF" valign="top">'.$val."</td>\n";
		$body .= "</tbody></tr>\n";
	} $rsa = null;
	$body .= '</table>';
	return $body;
}

//删除指定目录下的所有文件
function dir_clear($dir) { 
	$ret=true;
    $directory = dir($dir);                //创建一个dir类，用来读取目录中的每一个文件 
    while($entry = $directory->read()) {   //循环每一个文件,并取得文件名$entry 
        $filename = $dir.'/'.$entry;       //取得完整的文件名，带路径的 
        if(is_file($filename)) {           //如果是文件，则执行删除操作 
            $ret=@unlink($filename); 
        } 
    } 
    $directory->close();                   //关闭读取目录文件的类 
	return $ret;
} 

//弹出对话框和URL跳转,标题，默认和确定跳转的地址,内容，取消跳转的地址,自动调整的秒数(0为关闭)
function MessageBox($string = "",$url = "",$string2 = "",$autoturn = "自动关闭",$times = 5,$url2 = "",$buttons = 1)
{
	
	echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title></title></head><body>';
	echo '<script type="text/javascript" src="'.MICE_URL.MICE_TPL.'/admin/js/messagebox.js"></script>';
	echo "<Script language='JavaScript'>";
	if($string != "")
	{
		echo "MessageBox.show('$string','$string2',$buttons,'$url','$url2',$times,'$autoturn');";
	}
	if($url != "" and $string == "")
	{
		echo "location='".$url."';";
	}
	echo "</Script></body></html>";
}

function MiceStrstr($micefun,$str1,$str2){
	if(strstr($str1,$str2))
		return 1;
	else
		return 0;
}

//字符串重复函数
function MiceStrRepeat($micefun,$num){
	$strs='';
	for($i=1;$i<$num;$i++){
		$strs.='—';
	}
	return $strs;
}

function UpPostnums($micefun,$userid){
	$user=new model_User();
	$user->setSubsection('WHERE user_id='.$userid);
	$results=$user->doSelectOne();

	$user->setPostnums($results->getPostnums()+1);
	$user->doUpdate();

	return '';
}

//提取文章中的第一张图片的地址
function GetImgUrl($str)   
{   
  $ereg = '/<img.+src=\"?(.+\.(jpg|gif|bmp|bnp|png))\"?.+>/i';   

  if(preg_match_all($ereg,$str,$imgs))   
  {   
	  $path = $imgs[1];
	  $imgurl = str_replace(array("'","\"","\'","\\"),array('','','',''),$path[0]) ;
	  return $imgurl;   
  }   
  else   
  {   
	return false;   
  }   
}

//获取分类列表(SQL条件语句，起始记录数，偏移记录数)
function CategoryLists($micefun,$subsql,$nrows,$offset){		//Nrows,起始记录数，0为第一条,1为第2条
	$category=new model_Category();
	if($subsql){
		$category->setSubsection($subsql);
	}
	if($offset > 0){
		$category->setNrows($nrows);			//记录下标从0开始
		$category->setOffset($offset);
		$results=$category->doSelectLimit();
	}else{
		$results=$category->doSelect();
	}
	return $results;
}
?>